package com.sinodata.bsm.cicp.collector.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: 使用用户名和密码登陆主机，如果登陆成功就执行echo success命令，成功返回success表示登陆成功，其他或者抛出异常表示登陆失败
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-3-11 上午11:23:28          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class HostCanBeLogin extends Collector {

    @Override
    public String[] getPropMeta() {
        return new String[] { "$1" };
    }

    @Override
    public String[] getParamMeta() {
        return null;
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String cmd = "echo success";
        String[] propMetas = getPropMeta();
        BufferedReader reader = null;
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        try {
            reader = CommandReader.getReader(ip, cmd, (int) this.interval, 0);
            propValues[0] = ResPropertyValue.createPropValue("1", propMetas[0]);
        } catch (IOException e) {
            propValues[0] = ResPropertyValue.createPropValue("0", propMetas[0]);
        }
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;
    }

}
